package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.xxx.xbutler.data.RobotLoginMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.RobotLogin;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 二维码扫码状态回调接口
 * <p>
 * 扫码登录方式和缓存登录方式的中间状态，通过本回调接口返回给商家。
 * 状态1：待确认，代表用户已扫码，还没有点击移动端确认按钮；
 * 状态2：已确认，代表用户扫码后，在移动端已点击了确认按钮，此时用户需等待平台返
 * 平台通过nType:1001回调给商家后，可返回二维码的状态，商家可根据状态在自己的产品上做文案提示。
 *
 * <pre>
 * {
 *     "Data": {
 *         "nLogType": 0,
 *         "vcHeadUrl": "",
 *         "vcNickName": null,
 *         "nQrcodeStatus": -1,
 *         "vcBase64NickName": null,
 *         "vcRelationSerialNo": null
 *     },
 *     "nType": 1002,
 *     "nResult": 1,
 *     "vcResult": "Success",
 *     "nTimeStamp": 1739586975095,
 *     "vcSerialNo": "20250215103205732619617085196",
 *     "vcRobotWxId": "",
 *     "vcMerchantNo": "202407150008480",
 *     "vcRobotSerialNo": ""
 * }
 *
 * {
 *     "Data": {
 *         "nLogType": 0,
 *         "vcHeadUrl": "http://wx.qlogo.cn/mmhead/ver_1/duL6P7rffIVRFfBVpF6OlfQHqeQiblgPeKCQ1Qsy91KLZNj6nC9rYY98JcWsa2KgYicwq0JJPzkn5UK0sQpYxaibwdwBfdjKmwD3FbKwBiajs59YX9R3PK1xnuUu8aODojoD/0",
 *         "vcNickName": "邓斌",
 *         "nQrcodeStatus": 1,  // 1 待确认（扫码后移动端微信未点确认/老号自动触发缓存登录，待商家点击确认）2 已确认
 *         "vcBase64NickName": "6YKT5paM",
 *         "vcRelationSerialNo": null
 *     },
 *     "nType": 1002,
 *     "nResult": 1,
 *     "vcResult": "Success",
 *     "nTimeStamp": 1737779491795,
 *     "vcSerialNo": "20250125123049985113919500454",
 *     "vcRobotWxId": "",
 *     "vcMerchantNo": "202407150008480",
 *     "vcRobotSerialNo": ""
 * }
 *
 * {
 *     "Data": {
 *         "nLogType": 0,
 *         "vcHeadUrl": "http://wx.qlogo.cn/mmhead/ver_1/duL6P7rffIVRFfBVpF6OlfQHqeQiblgPeKCQ1Qsy91KLZNj6nC9rYY98JcWsa2KgYicwq0JJPzkn5UK0sQpYxaibwdwBfdjKmwD3FbKwBiajs59YX9R3PK1xnuUu8aODojoD/0",
 *         "vcNickName": "邓斌",
 *         "nQrcodeStatus": 2,
 *         "vcBase64NickName": "6YKT5paM",
 *         "vcRelationSerialNo": null
 *     },
 *     "nType": 1002,
 *     "nResult": 1,
 *     "vcResult": "Success",
 *     "nTimeStamp": 1737779504887,
 *     "vcSerialNo": "20250125123049985113919500454",
 *     "vcRobotWxId": "wxid_arpszpwvw10n21",
 *     "vcMerchantNo": "202407150008480",
 *     "vcRobotSerialNo": "14BC26C1F23A77A07760672146180AF5"
 * }
 * </pre>
 */
@Slf4j
@Component
public class _1002Handler implements CallbackHandler {

    @Resource
    private RobotLoginMapper robotLoginMapper;

    @Override
    public String name() {
        return "二维码扫码状态回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 1002;
    }

    @Override
    public void handle(Callback callback) {
        LocalDateTime now = LocalDateTime.now();
        JSONObject data = (JSONObject) callback.getData();
        String serialNo = callback.getSerialNo();
        LambdaUpdateWrapper<RobotLogin> wrapper = new LambdaUpdateWrapper<RobotLogin>()
                .set(RobotLogin::getQrcodeStatus, data.getInteger("nQrcodeStatus"))
                .set(RobotLogin::getRobotName, data.getString("vcNickName"))
                .set(RobotLogin::getBase64RobotName, data.getString("vcBase64NickName"))
                .set(RobotLogin::getUpdatedTime, now)
                .eq(RobotLogin::getSerialNo, serialNo);
        robotLoginMapper.update(wrapper);
    }
}
